<?php

/**
 * Created by PhpStorm.
 * User: fang
 * Date: 2017/12/26
 * Time: 下午5:36
 * 模板查询列表
 */
class TempletList
{
    static private $where;
    private $attr = ['has_cate','page_list','hash'];
    private $param = [];
    static private $field = '*';

    /**
     * 构造函数
     */
    public function __construct($data,$where,$field='*')
    {
        self::$where = self::get_where($data,$where);
        self::$field = $field;
    }

    /**
     * @param array $data
     * @param $where
     * @return string
     * 获取where语句
     */
    static private function get_where($data=[],$where){
        $intkeys = ['is_choice','lang_id','is_default','is_system'];
        $strkeys = ['sources','id'];
        $likekeys = ['name'];
        $inkeys = ['client_id'];
        $where .=  \app\common\model\Base::buildSearch($intkeys, $strkeys, [], $likekeys,$inkeys);
        if(!empty($data['keywords'])){
            $key_id  = \app\common\model\Base::keywords_cutout($data['keywords']);

            $where .= " and (name like '%{$data['keywords']}%' or id like '%{$data['keywords']}%'";
            if($key_id > 0){
                $where .= " or key_id like '%{$key_id}%'";
            }
            $where .= ")";
        }
        if(!empty($data['cate_id'])){
            $children_id = \app\assemble\model\ApplySysPage::get_assemble_cate_child([],$data['cate_id'],1);
            if(!empty($children_id)){
                $children_id .= ','.$data['cate_id'];
                $where .= " and c.cate_id in ({$children_id})";
            }else{
                $where .= " and c.cate_id=".$data['cate_id'];
            }
        }
        if(!empty($data['site_type'])){
            $where .= " and site_type in ({$data['site_type']})";
        }
        return $where;
    }

    /**
     * @param string $default_order 排序字段
     * @param string $asc 升序,降序
     * 设置排序
     */
    public function set_param($default_order='addtime',$asc='desc'){
        $this->param = \app\common\model\Base::list_param($default_order,$asc);
    }

    /**
     * @param string $default_order
     * @param string $asc
     * @return array|void
     * 获取排序值
     */
    public function get_param($default_order='addtime',$asc='desc'){
        if(empty($this->param)){
            $this->param = $this->set_param();
        }
    }

    /**
     * @param array $data
     * 设置属性
     */
    public function set_attr($data=[]){
        $this->attr = $data;
    }

    /**
     * @param array $data
     * @return array
     * 获取属性
     */
    public function get_attr($data=[]){
        return $this->attr;
    }
    public function search_list(){
        try{
            $this->get_param();
            $db_prefix = \app\assemble\controller\Base::get_db_prefix();
            if($this->check_attr('has_cate')){
                $list['total'] =  \think\Db::table($db_prefix.'templet')->alias('t')->join($db_prefix.'sys_templet_cate_relation c','c.assemble_id=t.id')->where(self::$where)->count();
                $list_tem = \think\Db::table($db_prefix.'templet')->alias('t')->join($db_prefix.'sys_templet_cate_relation c','c.assemble_id=t.id')->where(self::$where)->field(self::$field)->order($this->param['order_str'])->page($this->param['page'],$this->param['rows'])->select();
            }else{
                $list['total'] = \think\Db::table($db_prefix.'templet')->alias('t')->where(self::$where)->count();
                $list_tem = \think\Db::table($db_prefix.'templet')->alias('t')->where(self::$where)->field(self::$field)->order($this->param['order_str'])->page($this->param['page'],$this->param['rows'])->select();
            }
            if(!empty($list_tem)){
                $domain_web = $page_list = [];
                foreach($list_tem as $key=>$value){
                    $list_tem[$key]['templet_no'] = \app\assemble\model\Templet::get_templet_no($value['key_id']);

                    if($this->check_attr('page_list')){
                        //查找模板下面的所有页面
                        $page_list = model('assemble/Pages')->get_page_list(['templet_id'=>$value['id'],'web_id'=>$value['web_id'],'is_delete'=>0],'id,name,pid');
                        $list_tem[$key]['pages'] = $page_list??[];
                    }
                    if($this->check_attr('hash')) {
                        $hash = \app\assemble\model\Templet::templet_snop($value['id']);
                        $list_tem[$key]['templet_hash'] = !empty($hash['templet_id']) ? $hash['templet_id'] : '';
                        $list_tem[$key]['snap_hash'] = !empty($hash['hash_code']) ? $hash['hash_code'] : '';
                    }
                    if(empty($domain_web[$value['web_id']])){
                        $domain_web[$value['web_id']] = \app\common\model\Base::get_web_domain($value['web_id']);
                    }
                    $list_tem[$key]['domain'] = !empty($domain_web[$value['web_id']])?\app\common\model\Base::domain_preg($domain_web[$value['web_id']],0):'';
                    if($this->check_attr('preview_url') && !empty($list_tem[$key]['pages']) && !empty($value['client_id'])) {
                        $preview_url = !empty($domain_web[$value['web_id']])?\app\common\model\Base::domain_preg($domain_web[$value['web_id']],$value['client_id']):'';
                        $list_tem[$key]['preview_url'] = 'http://'.$preview_url.'/?page_id='.$list_tem[$key]['pages'][0]['id'].'&templet_id='.$value['id'];
                    }
                    //获取站点的所有页面分类
                    if($this->check_attr('has_cate')){
                        if(empty($assemble_relation[$value['web_id']])){
                            $assemble_relation[$value['web_id']] =  \app\assemble\model\Templet::get_assemble_relation_site(['web_id'=>$value['web_id'],'type'=>1]);
                        }
                        $list_tem[$key]['cate_list'] = [];
                        if(!empty($assemble_relation[$value['web_id']][$value['id']]['cate_id'])){
                            if(strpos($assemble_relation[$value['web_id']][$value['id']]['cate_id'],',') !== false){
                                $cate_list = explode(',',$assemble_relation[$value['web_id']][$value['id']]['cate_id']);
                                if(!empty($cate_list)){
                                    $new=[];
                                    foreach ($cate_list as $k=>$c){
                                        $new[] = (int)$c;
                                    }
                                    $list_tem[$key]['cate_list'] = $new;
                                    unset($cate_list,$new);
                                }
                            }else{
                                $list_tem[$key]['cate_list'] = [$assemble_relation[$value['web_id']][$value['id']]['cate_id']];
                            }
                        }
                        if(!empty($assemble_relation[$value['web_id']][$value['id']]['site_type'])){
                            $list_tem[$key]['site_type'] = $assemble_relation[$value['web_id']][$value['id']]['site_type'];
                        }
                    }
                    if($this->check_attr('qiniu_path') && !empty($value['thumb'])){
                        if(empty($qiniu_domain)){
                            $qiniu_domain = config('qiniu.DOMAIN');
                        }
                        $list_tem[$key]['thumb'] = $qiniu_domain.$value['thumb'];
                    }
                    unset($page_list,$hash);
                }
                unset($assemble_relation,$domain_web);
            }
            $list['list'] = $list_tem??[];
            unset($list_tem);
            return $list;
        }catch(\Exception $e){
            return false;
        }
    }
    private function check_attr(string $str){
        if(!empty($this->attr) && in_array($str,$this->attr)){
            return true;
        }
        return false;
    }
}